<?php

namespace app\common\model\mycurrency;

use think\Exception;
use think\Model;
use traits\model\SoftDelete;

class Device extends Model
{

    //use SoftDelete;

    // 表名
    protected $name = 'mycurrency_device';

    const STATUS_ZHENGCHENG = 1;//状态 = 正常
    const STATUS_JINYONG = 2;//状态 = 禁用

    const USE_STATUS_KONGXIAN = 1;//使用状态:1=空闲
    const USE_STATUS_SHIYONGZHONG = 2;//使用状态:2=使用中

    const IS_FAULT_WU = 1;//是否有故障 = 无;
    const IS_FAULT_YOU = 2;//是否有故障 = 有;

    const IS_ONLINE_BUZAIXIAN = 1;//是否在线 = 不在线
    const IS_ONLINE_ZAIXIAN = 2;//是否在线 = 在线


    /**
     * 验证设备是否可以租赁
     * @param int $device_id 设备id
     * @param int $device_number 设备编号
    */
    public static function canorcantLease($device_id = null,$device_number = null){
        $where = [
            'deletetime' => null,
        ];
        if($device_id){
            $where['id'] = $device_id;
        }elseif ($device_number){
            $where['number'] = $device_number;
        }else{
            throw new Exception('查询参数错误');
            return false;
        }
        $device = self::where($where)->find();
        if (!$device){
            throw new Exception('设备不存在');
            return false;
        }
        if($device['status'] == self::STATUS_JINYONG){
            throw new Exception('设备不可用');
            return false;
        }
        if($device['use_status'] == self::USE_STATUS_SHIYONGZHONG){
            throw new Exception('设备使用中');
            return false;
        }
        if($device['is_fault'] == self::IS_FAULT_YOU){
            throw new Exception('设备存在故障');
            return false;
        }
        if($device['is_online'] == self::IS_ONLINE_BUZAIXIAN){
            throw new Exception('设备离线');
            return false;
        }
        return $device;
    }




}
